package com.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.client.pojo.Products;
import com.client.service.ProductsService;
import com.client.utils.RedisUtils;
import com.client.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商城商品信息表 前端控制器
 * </p>
 *
 * @author ckick
 * @since 2025-09-20 08:28:54
 */
@RestController
public class ProductsController {
    @Autowired
    ProductsService productsService;
    @Autowired
    RedisUtils redisUtils;
    @RequestMapping("addProducts")
    ResponseMsg addProducts(@RequestBody Products products) {
        System.out.println("增加商品调用了");
        productsService.save(products);
        return ResponseMsg.success();
    }

    @RequestMapping("productsPage")
    ResponseMsg productsPage(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5") Integer limit) {
        System.out.println("分页查询调用了");
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("product_id");
        IPage<Products> productsIPage = productsService.page(new Page<>(page, limit), wrapper);
        return ResponseMsg.getInstance(200, productsIPage);
    }

    @RequestMapping("getGoodById")
    ResponseMsg getGoodById(Integer productId) {
        System.out.println("根据id查询了商品");
        String Rid=productId+"goods";
        if(redisUtils.get(Rid)==null){
            System.out.println("从数据库拿的数据");
            Products products = productsService.getById(productId);
            //products.setComments(iProductscommentService.getCommentsById(id));
            redisUtils.set(Rid,products);
            return ResponseMsg.getInstance(200, products);
        }
        System.out.println("从Redis拿的数据");
        Products products=(Products) redisUtils.get(Rid);
        return ResponseMsg.getInstance(200, products);
    }


}
